﻿using Furion;
using Furion.DatabaseAccessor;
using Furion.TaskScheduler;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Queer.AutoJob.Core.Entity;
using System.Linq;

namespace Queer.AutoJob
{
    [AppStartup(1)]
    public class JobAppStartup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {

        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // 延时启动定时模块，减少整体启动时间
            SpareTime.DoOnce(10000, (timer, count) =>
            {
                var serviceScopeFactory = App.GetService<IServiceScopeFactory>();
                using (var scope = serviceScopeFactory.CreateScope())
                {
                    var repository = scope.ServiceProvider.GetService<IRepository<AutoJobEntity>>();

                    // 仅获取非删除和非禁用的
                    var data = repository.Where(a => a.IsDelete == false && a.JobStatus == 1).ToList();

                    foreach (AutoJobEntity entity in data)
                        JobCenter.JobStart(entity,true);
                }
            });
        }

    }
}